import type {BasicEntity} from "@utils/entity";

let numTool = {
    /**
     * 生成随机数
     * @param min 最小值
     * @param max 最大值
     * @returns 返回随机数
     */
    randomInt: (min: number, max: number) => {
        return Math.floor(Math.random() * (max - min + 1)) + min
    },

    /**
     * 格式化金额展示
     * @param amount 金额
     * @param pre
     * @param end
     * @returns
     */
    formatMoney: (amount: number, pre = "", end = "") => {
        return `${pre}${(amount / 100).toFixed(2)}${end}`
    },

    /**
     * 判断是否相等
     * @param n1
     * @param n2
     */
    numbersCloseEnoughToEqual: (n1, n2) => {
        return Math.abs(n1 - n2) < Number.EPSILON;
    },
    /**
     * 求最大公约数
     * @param a
     * @param b
     */
    gcd: (a, b) => {
        let num1 = a;
        let num2 = b;
        while (num2 != 0) {
            let temp = num2
            num2 = num1 % num2
            num1 = temp
        }
        return num1
    },
    /**
     * 时间戳转天时分秒
     * @param timeSpace
     */
    calcDateSpace: (timeSpace) => {
        if (timeSpace && typeof timeSpace == 'number') {
            let dayStr = Math.floor(timeSpace / (24 * 3600 * 1000))
            let dayOther = timeSpace % (24 * 3600 * 1000)
            let hourStr = Math.floor(dayOther / (3600 * 1000))
            dayOther = dayOther % (3600 * 1000)
            let minuteStr = Math.floor(dayOther / (60 * 1000))
            dayOther = dayOther % (60 * 1000)
            let secondStr = Math.floor(dayOther / 1000)
            return `${dayStr}天 ${hourStr}时 ${minuteStr}分 ${secondStr}秒`
        } else {
            return "--"
        }
    },
    /**
     * 时间戳转天时分秒
     * @param timeSpace
     */
    calcDateSpaceWithObj: (timeSpace):BasicEntity => {
        if (timeSpace && typeof timeSpace == 'number') {
            let dayStr = Math.floor(timeSpace / (24 * 3600 * 1000))
            let dayOther = timeSpace % (24 * 3600 * 1000)
            let hourStr = Math.floor(dayOther / (3600 * 1000))
            dayOther = dayOther % (3600 * 1000)
            let minuteStr = Math.floor(dayOther / (60 * 1000))
            dayOther = dayOther % (60 * 1000)
            let secondStr = Math.floor(dayOther / 1000)
            return {
                day: String(dayStr).padStart(2, "0"),
                hour: String(hourStr).padStart(2, "0"),
                minute: String(minuteStr).padStart(2, "0"),
                second: String(secondStr).padStart(2, "0")
            }
        } else {
            return {}
        }
    },
}

export default numTool





